package top.mhtools.pigeon.common.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mhtools.pigeon.common.persistence.SendMessageRequest;
import top.mhtools.pigeon.common.service.SendMessageService;
import top.mhtools.pigeon.common.service.support.AbstractSendMessageExecuteStrategy;
import top.mhtools.pigeon.common.service.support.SendMessageExecuteChooser;

import java.util.Arrays;

/**
 * 消息发送
 *
 * @author machen
 * @date 2020/4/24 10:08
 */
@Service
public class SendMessageServiceImpl implements SendMessageService {

    @Autowired
    private SendMessageExecuteChooser sendMessageExecuteChooser;

    @Override
    public void sendMessage(SendMessageRequest messageRequest) {
        Arrays.asList(StrUtil.split(messageRequest.getSendType(), ",")).forEach(each -> execute(each, messageRequest));
    }

    private void execute(String flag, SendMessageRequest sendMessageRequest) {
        AbstractSendMessageExecuteStrategy messageExecuteStrategy = sendMessageExecuteChooser.choose(flag);
        messageExecuteStrategy.sendMessage(sendMessageRequest);
    }

}
